﻿Public Class clsSettings
    Private Shared m_pSettingsDict As Dictionary(Of String, Object)

    Private Const FILENAME As String = "settings.ini"
    Private Const ALLOCATIONCHAR As String = "="

    Private Shared Sub LoadSettingsFile(ByVal sFilePath As String, ByVal bOverwriteExisting As Boolean)
        If m_pSettingsDict Is Nothing Then
            m_pSettingsDict = New Dictionary(Of String, Object)
        End If

        If Not IO.File.Exists(sFilePath) Then Return

        Dim pStreamReader As IO.StreamReader = Nothing
        Dim pLines As New List(Of String)

        Try
            pStreamReader = New IO.StreamReader(sFilePath, System.Text.Encoding.Default)
            Do Until pStreamReader.EndOfStream
                pLines.Add(pStreamReader.ReadLine)
            Loop

        Catch ex As Exception
            clsLogWriter.WriteLog(LogLevel.ERROR, "Fehler beim Laden der Einstellungsdatei", ex)

        Finally
            If pStreamReader IsNot Nothing Then
                pStreamReader.Close()
                pStreamReader.Dispose()
            End If

        End Try

        Dim sKey = String.Empty, sValue As String = String.Empty

        For Each sLine In pLines
            sKey = sLine.Split(ALLOCATIONCHAR)(0)
            sValue = sLine.Split(ALLOCATIONCHAR)(1)
            If m_pSettingsDict.ContainsKey(sKey) Then
                If bOverwriteExisting Then
                    m_pSettingsDict(sKey) = sValue
                End If
                clsLogWriter.WriteLog(LogLevel.WARNING, "Fehlerhafte Einstellungsdatei (Mehrfachbelegung eines Schlüssels)", Nothing)

            Else
                m_pSettingsDict.Add(sKey, sValue)

            End If
        Next

    End Sub

    Public Shared ReadOnly Property SaveFilePath As String
        Get
            Return IO.Path.Combine(clsFunctions.GetUserAppFolder, FILENAME)
        End Get
    End Property

    Public Shared Sub SaveSettings()
        Dim sFilePath As String = IO.Path.Combine(clsFunctions.GetUserAppFolder, FILENAME)

        Dim pStreamWriter As IO.StreamWriter = Nothing
        Dim pSB As New System.Text.StringBuilder

        For Each pSetting In m_pSettingsDict
            pSB.Append(pSetting.Key).Append(ALLOCATIONCHAR).Append(pSetting.Value.ToString)
            pSB.AppendLine()
        Next

        Try
            pStreamWriter = New IO.StreamWriter(sFilePath, False, System.Text.Encoding.Default)

            pStreamWriter.Write(pSB.ToString)

        Catch ex As Exception
            clsLogWriter.WriteLog(LogLevel.ERROR, "Fehler beim Speichern der Einstellungsdatei", ex)

        Finally
            If pStreamWriter IsNot Nothing Then
                pStreamWriter.Close()
                pStreamWriter.Dispose()
            End If

        End Try
    End Sub

    Public Shared Property Setting(ByVal sSettingName As String) As Object
        Get
            If m_pSettingsDict Is Nothing Then
                LoadSettingsFile(SaveFilePath, True)
            End If

            If m_pSettingsDict.ContainsKey(sSettingName) Then
                Return m_pSettingsDict(sSettingName)
            Else
                Return Nothing
            End If

        End Get
        Set(ByVal value As Object)
            If m_pSettingsDict Is Nothing Then
                LoadSettingsFile(SaveFilePath, True)
            End If

            If m_pSettingsDict.ContainsKey(sSettingName) Then
                m_pSettingsDict(sSettingName) = value
            Else
                m_pSettingsDict.Add(sSettingName, value)
            End If
        End Set
    End Property

    Public Shared Sub SetDefaultSettings(ByVal bOverwriteExisting As Boolean)
        LoadSettingsFile("default_settings.ini", bOverwriteExisting)
    End Sub

    Public Shared Function SettingsFileExists() As Boolean
        Return IO.File.Exists(SaveFilePath)
    End Function
End Class
